# coding: utf-8


import os
import filecmp
import difflib
import shutil


def make_path_files():
    f01 = 'cmpfile1.txt'
    f02 = 'cmpfile2.txt'

    f11 = 'work1/cmpfile1.txt'
    f12 = 'work1/cmpfile2.txt'
    f13 = 'work1/cmpfile3.txt'

    f21 = 'work2/cmpfile1.txt'
    f22 = 'work2/cmpfile2.txt'

    f31 = 'work3/work1/cmpfile1.txt'
    f32 = 'work3/work1/cmpfile2.txt'
    f33 = 'work3/work2/cmpfile1.txt'
    f34 = 'work3/work3/cmpfile3.txt'

    f41 = 'work4/work1/cmpfile1.txt'
    f42 = 'work4/work2/cmpfile2.txt'
    f43 = 'work4/work2/cmpfile1.txt'
    f44 = 'work4/work3/cmpfile3.txt'

    lines1 = ['Hello-World\n', 'Hello-China\n']
    lines2 = ['Hello World\n', 'Hello-China\n']
    lines3 = ['Hello-World\n', 'Hello-China\n']

    lines21 = ['Hello\n', 'Hello-China\n']

    if os.path.isfile(f01):
        os.remove(f01)
    if os.path.isfile(f02):
        os.remove(f02)

    if os.path.isdir('work1'):
        shutil.rmtree('work1')
    if os.path.isdir('work2'):
        shutil.rmtree('work2')
    if os.path.isdir('work3'):
        shutil.rmtree('work3')
    if os.path.isdir('work4'):
        shutil.rmtree('work4')

    os.mkdir('work1')
    os.mkdir('work2')
    os.mkdir('work3')
    os.mkdir('work3/work1')
    os.mkdir('work3/work2')
    os.mkdir('work3/work3')
    os.mkdir('work4')
    os.mkdir('work4/work1')
    os.mkdir('work4/work2')
    os.mkdir('work4/work3')
    os.mkdir('work4/work4')

    # make work1,work2 files
    with open(f11, 'wt') as fp1, open(f12, 'wt') as fp2, open(f13, 'wt') as fp3, \
            open(f21, 'wt') as fp21, open(f22, 'wt') as fp22:
        fp1.writelines(lines1)
        fp2.writelines(lines2)
        fp3.writelines(lines3)
        fp21.writelines(lines21)
        fp22.writelines(lines2)

    # make work3 files
    for f, line in zip([f31, f32, f33, f34], [lines1, lines2, lines3, lines3]):
        with open(f, 'wt') as fp:
            fp.writelines(line)

    # make work4 files
    for f, line in zip([f41, f42, f43, f44], [lines1, lines2, lines1, lines3]):
        with open(f, 'wt') as fp:
            fp.writelines(line)

    # make f01, f02
    with open(f01, 'wt') as fp01, open(f02, 'wt') as fp02:
        fp01.writelines("this is line01\nthis is line02\nthis is line03\n")
        fp02.writelines("this is line0102\nthis is line02\nthis is line0203\nthis is line0204\n")


def filecmp_cmp():
    # filecmp.cmp
    print(f"# 使用filecmp.cmp进行文件比较\n"
          f" >>> filecmp.cmp('cmpfile1.txt', 'cmpfile2.txt', shallow=False)\n"
          f"{filecmp.cmp('work1/cmpfile1.txt', 'work1/cmpfile2.txt', shallow=False)}\n"
          )


def difflib_differ_compare():
    """
    使用filecmp.cmp及difflib.Differ().compare比较文件
    :return:
    """
    # # make f01, f02
    # with open(f01, 'wt') as fp01, open(f02, 'wt') as fp02:
    #     fp01.writelines("this is line01\nthis is line02\nthis is line03\n")
    #     fp02.writelines("this is line0102\nthis is line02\nthis is line0203\nthis is line0204\n")

    # difflib.Differ().compare
    with open('cmpfile1.txt') as fp1, open('cmpfile2.txt') as fp2:
        lines1 = fp1.readlines()
        lines2 = fp2.readlines()
        for line in difflib.Differ().compare(lines1, lines2):
            print(line, end='')
        # print(list(difflib.Differ().compare(lines1, lines2)))


def filecmp_cmpfiles():
    # cmpfiles目录比较
    cmpresult = filecmp.cmpfiles('work1', 'work2', common=['cmpfile1.txt', 'cmpfile2.txt', 'cmpfile3.txt'])
    print(""
          "# 使用filecmp.cmpfiles进行目录文件内容比较\n"
          " >>> filecmp.cmpfiles('.', '.', common=['cmpfile1.txt'])\n"
          f"{filecmp.cmpfiles('.', '.', common=['cmpfile1.txt'])}\n"
          " >>> filecmp.cmpfiles('work1', 'work2', common=['cmpfile1.txt', 'cmpfile2.txt', 'cmpfile3.txt'])\n"
          f"{cmpresult}\n"
          )


def filecmp_dircmp():
    # dircmp目录比较
    # --- 目录比较报告
    print("# 使用filecmp.dircmp进行目录比较\n"
          "# --- 目录比较报告\n"
          " >>> filecmp.dircmp('work1', 'work2').report()")
    filecmp.dircmp('work1', 'work2').report()

    # --- 全部闭包报告
    print("# --- 全部闭包报告\n"
          " >>> filecmp.dircmp('work3', 'work4').report_full_closure()")
    filecmp.dircmp('work3', 'work4').report_full_closure()

    # --- 比较报告
    print("\n# 目录比较报告")
    cmp = filecmp.dircmp('work3', 'work4')
    print(" >>> cmp = filecmp.dircmp('work3', 'work4')\n"
          " >>> cmp.__getattr__('subdirs')['work2'].report()")
    cmp.__getattr__('subdirs')['work2'].report()
    # --- 筛选比较
    print("\n# 通过设置ignore、hide，筛选比较报告")
    cmp_filter = filecmp.dircmp('work3', 'work4', ignore=['work1'], hide=['work3'])
    print(" >>> cmp_filter = filecmp.dircmp('work3', 'work4', ignore=['work1'], hide=['work3'])\n"
          " >>> cmp_filter.report()")
    cmp_filter.report()

    # --- 通过属性提取比较内容
    print(
        f"# 通过属性提取比较内容\n"
        f" >>> cmp = filecmp.dircmp('work3', 'work4')\n"
        f" >>> cmp.__getattr__('same_files')\n"
        f"{cmp.__getattr__('same_files')}\n"
        f" >>> cmp.__getattr__('diff_files')\n"
        f"{cmp.__getattr__('diff_files')}\n"
        f" >>> cmp.__getattr__('subdirs')\n"
        f"{cmp.__getattr__('subdirs')}\n"
        f" >>> cmp.__getattr__('subdirs')['work2'].report()\n"
        f"diff work3\work2 work4\work2\n"
        "Only in work4\work2 : ['cmpfile2.txt']\n"
        "Identical files : ['cmpfile1.txt']\n"
        f" >>> filecmp.dircmp('work3', 'work4', ignore=['work1'], hide=['work3']).report()\n"
        "diff work3 work4\n"
        "Only in work4: ['work4']\n"
        "Common subdirectories: ['work2']\n"
    )
    return


if __name__ != "__main__":
    make_path_files()
